/** 
 * BackupBO.java
 * Created on March 27, 2007, 3:39 PM
 *
 *
 */

package edu.byu.isys413.jjenkins.data;

import java.sql.Date;
import java.util.Calendar;

/**
 * The class is the business object for a backup. it creates attibutes, methods,
 * and functionallity pertaining to a backup.
 *
 * @author      Jeff Jenkins
 * @version     1.0
 */
public class BackupBO extends RevenueSourceBO {
    
    /**
     * size of backup
     */
    private double backupSize;
    /**
     * backup begin date
     */
    private Date beginDate;
    /**
     * backup end date
     */
    private Date endDate;
    /**
     * conceptual backup containing price per GB
     */
    private ConceptualBackupBO conceptualBackup;
    /**
     * transaction line
     */
    private TransactionLineBO line;
    
    /** Creates a new instance of BackupBO */
    public BackupBO() {
    }
    
    /**
     * the method creates the description of the backup
     * @return description
     */
    public String createDescription(){
        setDescription(getBackupSize() + "  GB             Backup Service         Date:  " + getBeginDate() + " to " + getEndDate());
        return getDescription();
        
    }
    
    /**
     * This method calculates the price of the backup
     * @return price
     */
    public double calcPrice(){
        setPrice(getConceptualBackup().getPrice() * getBackupSize() * getMonthsBetween(getBeginDate(), getEndDate()));
        return getPrice();
        
    }
    
    /**
     * this method gets the size
     * @return size
     */
    public double getBackupSize() {
        return backupSize;
    }
    
    /**
     * this method sets teh size
     * @param backupSize size
     */
    public void setBackupSize(double backupSize) {
        this.backupSize = backupSize;
    }
    
    /**
     * This method gets the begin date
     * @return begin date
     */
    public Date getBeginDate() {
        return beginDate;
    }
    
    /**
     * This method sets the begin date
     * @param beginDate begin date
     */
    public void setBeginDate(Date beginDate) {
        this.beginDate = beginDate;
    }
    
    /**
     * this method gets the end date
     * @return end date
     */
    public Date getEndDate() {
        return endDate;
    }
    
    /**
     * this method sets the end date of the backup
     * @param endDate backup end date
     */
    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }
    
    /**
     * This method sets the conceptual backup. The conceptual backup contains the current price per GB
     * @return backup's conceptual backup
     */
    public ConceptualBackupBO getConceptualBackup() {
        return conceptualBackup;
    }
    
    /**
     * This method sets the conceptual backup
     * @param conceptualBackup conceptual backup
     */
    public void setConceptualBackup(ConceptualBackupBO conceptualBackup) {
        this.conceptualBackup = conceptualBackup;
    }
    /**
     * this method calcualtes the date between two dates
     * @param d1 date 1
     * @param d2 date 2
     * @return months between
     */
    public int getMonthsBetween(Date d1, Date d2){
        Calendar c1=Calendar.getInstance();
        Calendar c2=Calendar.getInstance();
        int monthDiff=0;
        
        c1.setTime(d1);
        c2.setTime(d2);
        if (d2.before(d1)) {
            c1.setTime(d2);
            c2.setTime(d1);
        }
        while (c1.before(c2)) {
            c1.add(Calendar.MONTH,1);
            monthDiff++;
        }
        return monthDiff;
    }
    
    /**
     * This method adds months to a date
     * @return new date
     * @param d1 original date
     * @param length the number of months to add
     */
    public Date addMonths(Date d1, double length){
        
        Calendar c1=Calendar.getInstance();
        int monthDiff=0;
        
        c1.setTime(d1);
        
        for(int i = 0; i < length; i++) {
            c1.add(Calendar.MONTH,1);
            
        }
        return new Date(c1.getTimeInMillis());
        
    }

    /**
     * This method gets the transaction line
     * @return Transaction LIne
     */
    public TransactionLineBO getLine() {
        return line;
    }

    /**
     * This method sets the transaction line for the backup
     * @param line transaciton line of backup
     */
    public void setLine(TransactionLineBO line) {
        this.line = line;
    }
    
}
